package com.qdsg.ysg.doctor.service;


import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;


import androidx.annotation.Nullable;
import com.qdsg.ysg.doctor.R;


/**
 * created by liyh  on 2019/9/12
 **/
@SuppressLint("Registered")
public class FloatingService extends Service {
    public static boolean isStarted = false;

    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;

    private View view;
    private static final String TAG = "FloatingService";


    @Override
    public void onCreate() {
        super.onCreate();
        isStarted = true;
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        layoutParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        layoutParams.format = PixelFormat.RGBA_8888;
        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.width = 300;
        layoutParams.height = 500;
        layoutParams.x = 300;
        layoutParams.y = 300;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        long userid = intent.getLongExtra("userid", 0);
        showFloatingWindow(userid);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (view != null) {
            windowManager.removeView(view);
        }

    }

    private void showFloatingWindow(long userid) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.canDrawOverlays(this)) {
                view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.float_view_inner_layout, null);
                /*TextView tvclose = view.findViewById(R.id.tvclose);
                tvclose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.setAction("returnmeeting");
                        intent.putExtra("RETURNTOMEETING",0);
                        sendBroadcast(intent);
                    }
                });*/
//                RelativeLayout videoViewWrap = view.findViewById(R.id.videoViewWrap);
//                YSXMobileRTCVideoView mobileRTCVideoView = view.findViewById(R.id.iv_live_cover);
//                MobileRTCVideoViewManager mobileRTCVideoViewManager = mobileRTCVideoView.getVideoViewManager();
//                YSXMobileRTCVideoUnitRenderInfo rtcRenderInfo = new YSXMobileRTCVideoUnitRenderInfo(0, 0, 100, 100);
//                mobileRTCVideoViewManager.addAttendeeVideoUnit(userid, rtcRenderInfo);
                windowManager.addView(view, layoutParams);
                view.setOnTouchListener(new FloatingOnTouchListener());
            }
        }
    }

    private class FloatingOnTouchListener implements View.OnTouchListener {
        private int x;
        private int y;
        int downx = 0;
        int downy = 0;

        @Override
        public boolean onTouch(View view, MotionEvent event) {


            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x = (int) event.getRawX();
                    y = (int) event.getRawY();
                    downx = (int) event.getRawX();
                    downy = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int nowX = (int) event.getRawX();
                    int nowY = (int) event.getRawY();
                    int movedX = nowX - x;
                    int movedY = nowY - y;
                    x = nowX;
                    y = nowY;
                    layoutParams.x = layoutParams.x + movedX;
                    layoutParams.y = layoutParams.y + movedY;
                    windowManager.updateViewLayout(view, layoutParams);
                    break;
                case MotionEvent.ACTION_UP:
                    int upx = (int) event.getRawX();
                    int upy = (int) event.getRawY();
                    int finalmovedX = Math.abs(upx - downx);
                    int finalmovedY = Math.abs(upy - downy);
                    double moveddistance = Math.sqrt(Math.pow(finalmovedX, 2) + Math.pow(finalmovedY, 2));
                    Log.e(TAG, "onTouch:moveddistance " + moveddistance);
                    Log.e("Upx-Upy", "upx:"+upx+"upy:"+upx);
                    if (moveddistance < 20) {
                        Intent intent = new Intent();
                        intent.setAction("returnmeeting");
                        intent.putExtra("RETURNTOMEETING",1);
                        sendBroadcast(intent);
                    } else {
                        break;
                    }
                default:
                    break;
            }
            return false;
        }
    }

}